home *** CD-ROM | disk | FTP | other *** search
/ User's Choice Windows CD / User's Choice Windows CD (CMS Software)(1993).iso / utility1 / gs261src.zip / ZCONTROL.C < prev    next >
C/C++ Source or Header  |  1993-05-13  |  11KB  |  411 lines

  1. /* Copyright (C) 1989, 1992, 1993 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* zcontrol.c */
  20. /* Control operators for Ghostscript */
  21. #include "ghost.h"
  22. #include "errors.h"
  23. #include "oper.h"
  24. #include "estack.h"
  25. #include "iutil.h"
  26. #include "store.h"
  27.  
  28. /* Check for updating the currentfile cache. */
  29. #define esfile_check(ep)\
  30.   if ( r_has_type_attrs(ep, t_file, a_executable) ) esfile = 0
  31.  
  32. /* Forward references */
  33. private int no_cleanup(P1(os_ptr));
  34. private es_ptr find_stopped(P0());
  35. private void pop_estack_to(P2(os_ptr, es_ptr));
  36.  
  37. /* See the comment in opdef.h for an invariant which allows */
  38. /* more efficient implementation of for, loop, and repeat. */
  39.  
  40. /* <obj> exec - */
  41. int
  42. zexec(register os_ptr op)
  43. {    check_op(1);
  44.     check_estack(1);
  45.     ++esp;
  46.     ref_assign(esp, op);
  47.     esfile_check(esp);
  48.     pop(1);
  49.     return o_push_estack;
  50. }
  51.  
  52. /* <bool> <proc> if - */
  53. int
  54. zif(register os_ptr op)
  55. {    check_type(op[-1], t_boolean);
  56.     if ( op[-1].value.index )        /* true */
  57.        {    check_estack(1);
  58.         ++esp;
  59.         ref_assign(esp, op);
  60.         esfile_check(esp);
  61.        }
  62.     pop(2);
  63.     return o_push_estack;
  64. }
  65.  
  66. /* <bool> <proc_true> <proc_false> ifelse - */
  67. int
  68. zifelse(register os_ptr op)
  69. {    check_type(op[-2], t_boolean);
  70.     check_estack(1);
  71.     ++esp;
  72.     if ( op[-2].value.index )
  73.        {    ref_assign(esp, op - 1);
  74.        }
  75.     else
  76.        {    ref_assign(esp, op);
  77.        }
  78.     esfile_check(esp);
  79.     pop(3);
  80.     return o_push_estack;
  81. }
  82.  
  83. /* <init> <step> <limit> <proc> for - */
  84. private int
  85.   for_pos_int_continue(P1(os_ptr)),
  86.   for_neg_int_continue(P1(os_ptr)),
  87.   for_real_continue(P1(os_ptr));
  88. int
  89. zfor(register os_ptr op)
  90. {    int code;
  91.     float params[3];
  92.     register es_ptr ep;
  93.     check_proc(*op);
  94.     if ( r_has_type(op - 1, t_integer) &&
  95.          r_has_type(op - 2, t_integer) &&
  96.          r_has_type(op - 3, t_integer)
  97.        )
  98.         code = 7;
  99.     else if ( (code = num_params(op - 1, 3, params)) < 0 )
  100.         return code;
  101.     check_estack(7);
  102.     /* Push a mark, the control variable, the initial value, */
  103.     /* the increment, the limit, and the procedure, */
  104.     /* and invoke the continuation operator. */
  105.     mark_estack(es_for, no_cleanup);
  106.     ep = esp += 5;
  107.     if ( (code & 3) == 3 )        /* initial & increment are ints */
  108.        {    ep[-4] = op[-3];
  109.         ep[-3] = op[-2];
  110.         if ( code == 7 )
  111.             ep[-2] = op[-1];
  112.         else
  113.             make_int(ep - 2, (long)params[2]);
  114.         if ( ep[-3].value.intval >= 0 )
  115.             make_op_estack(ep, for_pos_int_continue);
  116.         else
  117.             make_op_estack(ep, for_neg_int_continue);
  118.        }
  119.     else
  120.        {    make_real(ep - 4, params[0]);
  121.         make_real(ep - 3, params[1]);
  122.         make_real(ep - 2, params[2]);
  123.         make_op_estack(ep, for_real_continue);
  124.        }
  125.     ep[-1] = *op;
  126.     pop(4);
  127.     return o_push_estack;
  128. }
  129. /* Continuation operators for for, separate for positive integer, */
  130. /* negative integer, and real. */
  131. /* Execution stack contains mark, control variable, increment, */
  132. /* limit, and procedure (procedure is topmost.) */
  133. /* Continuation operator for positive integers. */
  134. private int
  135. for_pos_int_continue(register os_ptr op)
  136. {    register es_ptr ep = esp;
  137.     long var = ep[-3].value.intval;
  138.     if ( var > ep[-1].value.intval )
  139.        {    esp -= 5;    /* pop everything */
  140.         return o_pop_estack;
  141.        }
  142.     push(1);
  143.     make_int(op, var);
  144.     ep[-3].value.intval = var + ep[-2].value.intval;
  145.     ref_assign(ep + 2, ep);        /* saved proc */
  146.     esp = ep + 2;
  147.     return o_push_estack;
  148. }
  149. /* Continuation operator for negative integers. */
  150. private int
  151. for_neg_int_continue(register os_ptr op)
  152. {    register es_ptr ep = esp;
  153.     long var = ep[-3].value.intval;
  154.     if ( var < ep[-1].value.intval )
  155.        {    esp -= 5;    /* pop everything */
  156.         return o_pop_estack;
  157.        }
  158.     push(1);
  159.     make_int(op, var);
  160.     ep[-3].value.intval = var + ep[-2].value.intval;
  161.     ref_assign(ep + 2, ep);        /* saved proc */
  162.     esp = ep + 2;
  163.     return o_push_estack;
  164. }
  165. /* Continuation operator for reals. */
  166. private int
  167. for_real_continue(register os_ptr op)
  168. {    es_ptr ep = esp;
  169.     float var = ep[-3].value.realval;
  170.     float incr = ep[-2].value.realval;
  171.     if ( incr >= 0 ? (var > ep[-1].value.realval) :
  172.         (var < ep[-1].value.realval) )
  173.            {    esp -= 5;    /* pop everything */
  174.             return o_pop_estack;
  175.            }
  176.     push(1);
  177.     ref_assign(op, ep - 3);
  178.     ep[-3].value.realval = var + incr;
  179.     esp = ep + 2;
  180.     ref_assign(ep + 2, ep);        /* saved proc */
  181.     return o_push_estack;
  182. }
  183.  
  184. /* <int> <proc> repeat - */
  185. private int repeat_continue(P1(os_ptr));
  186. int
  187. zrepeat(register os_ptr op)
  188. {    check_type(op[-1], t_integer);
  189.     check_proc(*op);
  190.     if ( op[-1].value.intval < 0 )
  191.         return_error(e_rangecheck);
  192.     check_estack(5);
  193.     /* Push a mark, the count, and the procedure, and invoke */
  194.     /* the continuation operator. */
  195.     mark_estack(es_for, no_cleanup);
  196.     *++esp = op[-1];
  197.     *++esp = *op;
  198.     make_op_estack(esp + 1, repeat_continue);
  199.     pop(2);
  200.     return repeat_continue(op - 2);
  201. }
  202. /* Continuation operator for repeat */
  203. private int
  204. repeat_continue(register os_ptr op)
  205. {    es_ptr ep = esp;        /* saved proc */
  206.     if ( --(ep[-1].value.intval) >= 0 )    /* continue */
  207.        {    esp += 2;
  208.         ref_assign(esp, ep);
  209.         return o_push_estack;
  210.        }
  211.     else                /* done */
  212.        {    esp -= 3;        /* pop mark, count, proc */
  213.         return o_pop_estack;
  214.        }
  215. }
  216.  
  217. /* <proc> loop */
  218. private int loop_continue(P1(os_ptr));
  219. int
  220. zloop(register os_ptr op)
  221. {    check_proc(*op);
  222.     check_estack(4);
  223.     /* Push a mark and the procedure, and invoke */
  224.     /* the continuation operator. */
  225.     mark_estack(es_for, no_cleanup);
  226.     *++esp = *op;
  227.     make_op_estack(esp + 1, loop_continue);
  228.     pop(1);
  229.     return loop_continue(op - 1);
  230. }
  231. /* Continuation operator for loop */
  232. private int
  233. loop_continue(register os_ptr op)
  234. {    register es_ptr ep = esp;        /* saved proc */
  235.     ref_assign(ep + 2, ep);
  236.     esp = ep + 2;
  237.     return o_push_estack;
  238. }
  239.  
  240. /* - exit - */
  241. int
  242. zexit(register os_ptr op)
  243. {    es_ptr ep = esp;
  244.     esfile = 0;        /* be lazy, just clear the cache */
  245.     while ( ep >= esbot )
  246.        {    if ( r_is_estack_mark(ep) )
  247.             switch ( estack_mark_index(ep--) )
  248.                {
  249.             case es_for:
  250.                 pop_estack_to(op, ep);
  251.                 return o_pop_estack;
  252.             case es_stopped:
  253.                 return_error(e_invalidexit);    /* not a loop */
  254.                }
  255.         else
  256.             ep--;
  257.        }
  258.     /* Return e_invalidexit if there is no mark at all. */
  259.     /* This is different from PostScript, which aborts. */
  260.     /* It shouldn't matter in practice. */
  261.     return_error(e_invalidexit);
  262. }
  263.  
  264. /* - stop - */
  265. int
  266. zstop(register os_ptr op)
  267. {    es_ptr ep = find_stopped();
  268.     esfile = 0;        /* be lazy, just clear the cache */
  269.     if ( ep )
  270.     {    pop_estack_to(op, ep - 1);
  271.         push(1);
  272.         make_bool(op, 1);
  273.         return o_pop_estack;
  274.     }
  275.     /* Return e_invalidexit if there is no mark at all. */
  276.     /* This is different from PostScript, which aborts. */
  277.     /* It shouldn't matter in practice. */
  278.     return_error(e_invalidexit);
  279. }
  280.  
  281. /* <obj> stopped <bool> */
  282. int
  283. zstopped(register os_ptr op)
  284. {    check_op(1);
  285.     /* Mark the execution stack, and push a false in case */
  286.     /* control returns normally. */
  287.     check_estack(3);
  288.     mark_estack(es_stopped, no_cleanup);
  289.     ++esp; make_false(esp);
  290.     *++esp = *op;            /* execute the operand */
  291.     esfile_check(esp);
  292.     pop(1);
  293.     return o_push_estack;
  294. }
  295.  
  296. /* - .instopped <bool> */
  297. int
  298. zinstopped(register os_ptr op)
  299. {    push(1);
  300.     make_bool(op, find_stopped() != 0);
  301.     return 0;
  302. }
  303.  
  304. /* - countexecstack <int> */
  305. int
  306. zcountexecstack(register os_ptr op)
  307. {    push(1);
  308.     make_int(op, esp - esbot + 1);
  309.     return 0;
  310. }
  311.  
  312. /* <array> execstack <subarray> */
  313. private int execstack_continue(P1(os_ptr));
  314. int
  315. zexecstack(register os_ptr op)
  316. {    /* We can't do this directly, because the interpreter */
  317.     /* might have cached some state.  To force the interpreter */
  318.     /* to update the stored state, we push a continuation on */
  319.     /* the exec stack; the continuation is executed immediately, */
  320.     /* and does the actual transfer. */
  321.     int depth = esp - esbot + 1;
  322.     check_write_type(*op, t_array);
  323.     if ( depth > r_size(op) )
  324.         return_error(e_rangecheck);
  325.     check_estack(1);
  326.     r_set_size(op, depth);
  327.     push_op_estack(execstack_continue);
  328.     return o_push_estack;
  329. }
  330. /* Continuation operator to do the actual transfer */
  331. private int
  332. execstack_continue(register os_ptr op)
  333. {    int depth = r_size(op);        /* was set above */
  334.     return refcpy_to_old(op, 0, esbot, depth, "execstack");
  335. }
  336.  
  337. /* <int> .quit - */
  338. int
  339. zquit(register os_ptr op)
  340. {    check_type(*op, t_integer);
  341.     return e_Quit;        /* Interpreter will do the exit */
  342. }
  343.  
  344. /* ------ Non-operator routines ------ */
  345.  
  346. /* Test whether we are inside a `stopped'. */
  347. /* The top level of the interpreter uses this. */
  348. int
  349. in_stopped(void)
  350. {    return find_stopped() != 0;
  351. }
  352.  
  353. /* ------ Initialization procedure ------ */
  354.  
  355. op_def zcontrol_op_defs[] = {
  356.     {"0countexecstack", zcountexecstack},
  357.     {"1exec", zexec},
  358.     {"0execstack", zexecstack},
  359.     {"0exit", zexit},
  360.     {"2if", zif},
  361.     {"3ifelse", zifelse},
  362.     {"0.instopped", zinstopped},
  363.     {"4for", zfor},
  364.     {"1loop", zloop},
  365.     {"1.quit", zquit},
  366.     {"2repeat", zrepeat},
  367.     {"0stop", zstop},
  368.     {"1stopped", zstopped},
  369.         /* Internal operators */
  370.     {"0%execstack_continue", execstack_continue},
  371.     {"0%for_pos_int_continue", for_pos_int_continue},
  372.     {"0%for_neg_int_continue", for_neg_int_continue},
  373.     {"0%for_real_continue", for_real_continue},
  374.     {"0%loop_continue", loop_continue},
  375.     {"0%repeat_continue", repeat_continue},
  376.     op_def_end(0)
  377. };
  378.  
  379. /* ------ Internal routines ------ */
  380.  
  381. /* Vacuous cleanup routine */
  382. private int
  383. no_cleanup(os_ptr op)
  384. {    return 0;
  385. }
  386.  
  387. /* Find a `stopped' mark on the e-stack. */
  388. /* Return the e-stack pointer or 0. */
  389. private es_ptr
  390. find_stopped(void)
  391. {    register es_ptr ep;
  392.     for ( ep = esp; ep >= esbot; --ep )
  393.       if ( r_is_estack_mark(ep) && estack_mark_index(ep) == es_stopped )
  394.         return ep;
  395.     return 0;
  396. }
  397.  
  398. /* Pop the e-stack, executing cleanup procedures as needed. */
  399. private void
  400. pop_estack_to(os_ptr op, es_ptr epstop)
  401. {    register es_ptr ep = esp;
  402.     while ( ep > epstop )
  403.     {    ep--;
  404.         if ( r_is_estack_mark(ep + 1) )
  405.         {    esp = ep;
  406.             (*real_opproc(ep + 1))(op);
  407.         }
  408.     }
  409.     esp = ep;
  410. }
  411.